package com.feonix.setup.controller;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.feonix.common.BaseInfo;
import com.feonix.common.util.LogUtil;
import com.feonix.common.util.MD5Util;
import com.feonix.common.util.Properties;
import com.feonix.common.util.StringUtil;
import com.feonix.common.util.mvc.Script;
import com.feonix.complat.constant.Settings;

@Controller
@RequestMapping("setup/main")
public class BaseSetupController {

	protected final Log logger = LogFactory.getLog(getClass());

	@RequestMapping("index")
	public ModelAndView main() {
		ModelAndView modelAndView = new ModelAndView("setup/main");
		modelAndView.addObject("baseSetup", "basesetup");
		modelAndView.addObject("dbSetup", "db/dbsetup");
		modelAndView.addObject("center_url", "basesetup");
		modelAndView.addObject("logout_url", "../logout");
		return modelAndView;
	}

	@RequestMapping("basesetup")
	public ModelAndView baseSetup() {
		Properties setupProp = getSetupProp();
		Properties versionProp = getVersionProp();
		ModelAndView modelAndView = new ModelAndView("setup/basesetup");
		modelAndView.addObject("url", "save");
		modelAndView.addObject("appPath", BaseInfo.getRealPath());
		modelAndView.addObject("kick", setupProp.getString("kick"));
		modelAndView.addObject("debug", setupProp.getString("debug"));
		modelAndView.addObject("domain", setupProp.getString("domain"));
		String appName = versionProp.getString("appname");
		modelAndView.addObject("appname", appName);
		modelAndView.addObject("version", versionProp.getString("version"));
		modelAndView.addObject("checkPasswordLevel", Settings.getSettings().getCheckLevel());
		return modelAndView;
	}

	@RequestMapping("save")
	@ResponseBody
	public String save(BaseSetupFormBean setupFormBean) {
		Properties properties = getSetupProp();
		properties.setProperty("kick", setupFormBean.getKick());
		properties.setProperty("debug", setupFormBean.getDebug());
		// properties.setProperty("appname", setupForm.getAppName());
		if (StringUtil.isNotEmpty(setupFormBean.getPassword())) {
			properties.setProperty("adminpw", MD5Util.encodePwd(setupFormBean.getPassword()));
		}
		String domain = setupFormBean.getDomain();
		if (StringUtil.isNotEmpty(domain)) {
			domain = StringUtil.convertPath(domain.trim().toLowerCase(), false);
			if (domain.startsWith("http") && !domain.endsWith("/")) {
				properties.setProperty("domain", domain);
			}
		}
		properties.setProperty("prepared", true);
		properties.save();
		saveLogLevel(setupFormBean.getDebug());
		
		// 保存jis设置

		BaseInfo.loadSystemConfig();
		Script script = Script.getInstanceWithJsLib();
		script.addAlert("保存成功").reloadParent();
		return script.getScript();
	}

	private Properties getSetupProp() {
		String dbProp = BaseInfo.getClassPath() + "pub/config/setup.properties";
		Properties properties = new Properties(dbProp);
		return properties;
	}

	private Properties getVersionProp() {
		String dbProp = BaseInfo.getClassPath() + "version.properties";
		Properties properties = new Properties(dbProp);
		return properties;
	}

	private void saveLogLevel(int logLevle) {
		String logpath = BaseInfo.getClassPath() + "log4j.properties";
		Properties properties = new Properties(logpath);
		properties.setProperty("log4j.logger.com.feonix", LogUtil.getLogLevelName(logLevle, 1));
		properties.setProperty("log4j.logger.jdbc.sqlonly", LogUtil.getLogLevelName(logLevle, 2));
		LogUtil.changeLogLevel(logLevle);
		properties.save();
	}
}
